﻿using AiQiuQuan.Sport.Core.Tool;
using AiQiuQuan.Sport.Model.Enums;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.WebApi.Entities;
using Microsoft.EntityFrameworkCore;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 俱乐部服务
    /// </summary>
    public partial class ClubService
    {
        #region private methods
        /// <summary>
        /// 设置俱乐部信息
        /// </summary>
        private async Task SetVenueInfoAsync(ClubDto club, decimal lat, decimal lng)
        {
            var venue = await _venueInfoRepository.AsNoTracking().FirstOrDefaultAsync(m => m.ID == club.VenueId);
            if (venue != null)
            {
                club.VenueName = venue.VI_Name;
                club.Lng = venue.VI_Lng.GetValueOrDefault();
                club.Lat = venue.VI_Lat.GetValueOrDefault();
            }

            club.Distance = DistanceTool.GetDistanceKM(lat, lng, club.Lat, club.Lng);
        }

        /// <summary>
        /// 俱乐部活动信息设置
        /// </summary>
        private async Task<Activity> CreateVenueActivityAsync(ClubActivityAddDto model)
        {
            var activity = _mapper.Map<Activity>(model);
            activity.ClubId = model.ClubId;
            activity.IsClub = true;
            activity.State = GameStateEnum.Joining;
            var club = await _clubRepository.AsNoTracking().FirstOrDefaultAsync(m => m.ID == activity.ClubId.Value);
            activity.Host = club?.Name;
            activity.Phone = club?.Phone;
            activity.Logo = club?.Logo;
            activity.ImgUrl = club?.ImageUrl;
            var venue = await _venueInfoRepository.AsNoTracking().FirstOrDefaultAsync(m => m.ID == club.VenueId);
            if (venue != null)
            {
                activity.ProvinceId = venue.ProvinceID;
                activity.ProvinceName = venue.VI_P_Name;
                activity.CityId = venue.CityID;
                activity.CityName = venue.VI_C_Name;
                activity.Lat = venue.VI_Lat.GetValueOrDefault();
                activity.Lng = venue.VI_Lng.GetValueOrDefault();
            }

            return activity;
        }
        #endregion
    }
}
